package com.example.main.tab

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import com.amap.api.maps.model.MyLocationStyle
import com.example.main.R
import dagger.internal.DoubleCheck
import me.majiajie.pagerbottomtabstrip.internal.RoundMessageView
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem

/**
 * 普通TabView
 */
class NormalTabView @JvmOverloads constructor(context: Context,attrs:AttributeSet?=null,defaultStyle: Int=0) :BaseTabItem (context, attrs,defaultStyle){
    private lateinit var mIcon:ImageView
    private lateinit var mTitle:TextView
    private lateinit var mMsg:RoundMessageView
    private lateinit var mDefaultIcon:Drawable
    private lateinit var mCheckedIcon:Drawable
    private lateinit var mtabTitle:String
    private var mchecked=false
    private var mMsgNum=0
    private var mDefaultTextColor=0x56000000
    private var mCheckedTextColor=0x56000000
    init {
        //加载布局
        LayoutInflater.from(context).inflate(R.layout.tab_normal_layout,this,true)
        //获取UI组件
        mIcon = findViewById(R.id.tab_icon)
        mTitle = findViewById(R.id.tab_title)
        mMsg = findViewById(R.id.tab_msg)
    }

    /**
     * 初始化数据
     */
    fun initData(@DrawableRes defaultIcon:Int,@DrawableRes checkedIcon:Int,title:String,tabChecked:Boolean=false,msgNum:Int=0){
        mDefaultIcon=ContextCompat.getDrawable(context,defaultIcon)!!
        mCheckedIcon=ContextCompat.getDrawable(context,checkedIcon)!!
        mtabTitle=title
        mchecked=tabChecked
        mMsgNum=msgNum
        mTitle.text=mtabTitle
//        mIcon.setImageDrawable(if (checked) mCheckedIcon else mDefaultIcon)
//        mTitle.setTextColor(if (checked) mCheckedTextColor else mDefaultTextColor)
//        mMsg.setMessageNumber(mMsgNum)
    }
    /**
     * 使用@DrawableRes， @ColorRes，@StringRes注解约束参数必须为Res下的资源
     */
    fun initTabData(title: String, @DrawableRes defaultIcon: Int, @DrawableRes checkedIcon: Int, @ColorRes defaultTextColor: Int, @ColorRes checkedTextColor: Int, tabChecked: Boolean = false, msgNum: Int = 0 ){
        //标题
        mtabTitle = title
        mTitle.text = mtabTitle

        //图标
        mDefaultIcon = ContextCompat.getDrawable(context, defaultIcon)!!
        mCheckedIcon = ContextCompat.getDrawable(context, checkedIcon)!!

        mchecked = tabChecked
        mMsgNum = msgNum

        //文本颜色
        mDefaultTextColor = ContextCompat.getColor(context, defaultTextColor)
        mCheckedTextColor = ContextCompat.getColor(context, checkedTextColor)

    }


    override fun setChecked(checked: Boolean) {
        mIcon.setImageDrawable(if (checked) mCheckedIcon else mDefaultIcon)
        mTitle.setTextColor(if (checked) mCheckedTextColor else mDefaultTextColor)
        mchecked = checked
    }

    override fun setMessageNumber(number: Int) {
        mMsg.messageNumber = number
        mMsgNum = number
    }

    override fun setHasMessage(hasMessage: Boolean) {
        mMsg.setHasMessage(hasMessage)
    }

    override fun setTitle(title: String) {
        mTitle.text = title
        mtabTitle = title
    }

    override fun setDefaultDrawable(drawable: Drawable) {
        mDefaultIcon = drawable
        if (!mchecked){
            mIcon.setImageDrawable(mDefaultIcon)
        }
    }

    override fun setSelectedDrawable(drawable: Drawable) {
        mCheckedIcon = drawable
        if (mchecked){
            mIcon.setImageDrawable(mCheckedIcon)
        }
    }

    override fun getTitle(): String {
        return mTitle.text.toString()
    }
}